﻿var MobileCfg = Class.create();

/*
var mobile_status;
var mobile_maillist;
var mobile_allalert;
var mobile_onTimeAlert;
var mobile_payPermsg;
var mobile_subjectlist;
*/
MobileCfg.prototype = {
	initialize: function() {
		this.mobile_status;
		this.mobile_maillist;
		this.mobile_allalert;
		this.mobile_onTimeAlert;
		this.mobile_payPermsg;
		this.mobile_subjectlist;
		this.isCache = false;
	},
	
	// 初始化手机邮件的事件绑定
	initEvent: function() {
		var self = this;
		var _tmp_dl = [
			{name: 's_mb_inbox', exec: function() {
				changePanel('mailList', 1);
			}},
			{name: 's_mb_query', exec: function() {
				self.searchMobileStatus();
			}},
			{name: 's_mb_cancel', exec: function() {
				self.closeMobileService();
			}},
			{name: 's_mb_set', exec: function() {
				self.getSms();
			}},
			{name: 's_mb_advance', exec: function() {
				self.getSmsOption();
			}},
			{name: 's_mb_main', exec: function() {
				self.getMobile();
			}},
			{name: 's_mb_setsms', exec: function() {
				self.setSms();
			}},
			{name: 's_mb_setoption', exec: function() {
				self.setSmsOption();
			}},
			{name: 's_mb_setoption', exec: function() {
				self.setSmsOption();
			}},
			{name: 's_mb_change', exec: function() {
				self.changeMoBile();
			}},
			{name: 's_mb_openservice', exec: function() {
				self.openMobileService();
			}}
		];
		Event.observe(DOM.mobileConfiguration, 'click', function(event) {
			var EL = $(event.target);
			try {
				_tmp_dl.each(function(item){
					if (Element.hasClassName(EL, item.name) || Element.hasClassName(EL.parentNode, item.name)) {
						try {
							item.exec();
						} catch(e) {}
					}
					var e = Element.hasClassName(EL, item.name) ? EL : Element.hasClassName(EL.parentNode, item.name) ? EL.parentNode : null;
					if(e && e.nodeName.toLowerCase() == 'a') {
						Event.stop(event);
					}
					return false;
				});
			} catch(e) {}
			return false;
		});
	},
	
    getMobile: function(){
        // _defun('http://mail.sohu.com/mo/MobileUserInfo', 'get', {}, this.getMobileCallBack.bind(this));
		this._get_();
    },
	
	// 获取手机邮件数据的接口
	_get_: function(callback) {
		_defun('/mo/MobileUserInfo', 'get', {}, function(ret) {
			if (callback && typeof callback == 'function') {
				callback(ret);
			} else {
				this.getMobileCallBack(ret);
			}
		}.bind(this));
	},
	// 设置缓存
	_set_cache: function(ret) {
		this.isCache = true;
	    this.mobile_status = ret.status;
	    this.mobile_maillist = ret.maillist;
	    this.mobile_allalert = ret.allalert;
	    this.mobile_onTimeAlert = ret.onTimeAlert;
	    this.mobile_payPermsg = ret.payPermsg;
	    this.mobile_subjectlist = ret.subjectlist;
	},
	
	getMobileCallBack: function(ret) {
		// ret 内容：{"maillist": "", "ontimealert": "1", "allsms": true, "paypermsg": "-1", "mobile": "13810606675", "subjectlist": "", "status": 0, "mobileuser": 0} 
		// status 0:成功， -1:查询失败, -2 参数失败
		// mobileuser 0:非手机邮件用户   1:邮件邮件用户
		
		//ret.mobile='15801207618';
		//ret.mobileuser=1;
		//ret.status=0;
		this._set_cache(ret);
	    
		var t = new Template(ExecFunc("getMobileTemplate",[],mobileMailCfgTemplate));
		var data = [];
		var urlHelp = "http://mail.sohu.com/momail/mobileHelp.html";
		if (USE_SYS.name=="vip.sohu"){
			urlHelp = "http://vip.sohu.com/mo/help/mobileHelp.html";
		}
		//手机邮件用户
	    if(ret.mobileuser == 1){ 

	    	data['mobile_info'] = '<dd>您的邮箱'+ UserSohuID + '已开通了手机邮件。</dd>'
	    									+'<dd>开通的手机号：'+ret.mobile.substring(0,3)+"****"+ret.mobile.substring(7)   
	    									+'&nbsp;&nbsp;<input type="submit" name="button" id="button" value="更改手机号" class="s_mb_change" />'
	    									+'</dd>'
	    									+'<dd><a href="'+urlHelp+'" target="_blank" hidefocus="true">详情请见&gt;&gt; 帮助</a></dd>';	
		}else{	//非手机邮件用户
			data['mobile_info']='<dt>手机闪电邮件服务实行包月计费，每月只需15元</dt>'
					+'<dd><input type="submit" name="button" id="button" value="立即开通手机邮件 " class="s_mb_openservice" /></dd>'
					+'<dd><a  href="'+urlHelp+'" target="_blank" hidefocus="true">详情请见&gt;&gt; 帮助</a></dd>';	
		}
		var tt = t.evaluate(data);
		hiddenAllPanel();
		changeToolbarStyle();
		DOM.mobileConfiguration.innerHTML = tt;
		DOM.mobileConfiguration.show();
		addAjaxHistory("mobileConfig", "changePanel('mobileConfiguration');", "手机邮件设置");
	},
	
	// 初始化wap邮箱的相关信息
	initWap: function() {
		hiddenAllPanel();
		DOM.mobileConfiguration.innerHTML = wapTemplate;
		DOM.mobileConfiguration.show();
		addAjaxHistory("wapConfig", "changePanel('wapConfiguration');", "wap邮箱");
	},
	
	getSms: function(r) {
		if(!this.isCache && !r) {
			var sf = arguments.callee;
			this._get_(function(ret) {
				this._set_cache(ret);
				sf.apply(this, [true]);
			}.bind(this));
			return;
		}
		if(this.mobile_status == 0){
			var t = new Template(mobileSmsTemplate);
			var data = [];

			if(this.mobile_allalert == 0) {
				data['mobile_remind'] = '<input name="mobile_remind" id="mobile_remind_y" type="radio" value="mobile_remind"  /> 打开 <input type="radio" id="mobile_remind_n" name="mobile_remind" value="mobile_remind" checked="checked" /> 关闭 ';
			} else {
				data['mobile_remind'] = '<input name="mobile_remind" id="mobile_remind_y" type="radio" value="mobile_remind"  checked="checked"  /> 打开 <input type="radio" name="mobile_remind" id="mobile_remind_n" value="mobile_remind" /> 关闭 ';
			}
			/*
			data['mobile_filter'] = ret.fromlist;
			data['mobile_subject'] = ret.subjectlist;
			*/
			data['mobile_filter'] = this.mobile_maillist;
			data['mobile_subject'] = this.mobile_subjectlist;
			
			var tt = t.evaluate(data);
			hiddenAllPanel();
			$('mobileConfiguration').update(tt);
			$('mobileConfiguration').show();
			// addAjaxHistory("mobileGetSms", "mobileCfg.getSms();", "手机邮件服务设置", is_rsh);
		} else {
			_dialog.alert({info: '请先开通手机邮件'});
		}
	},

	getSmsOption: function() {
		if(!this.isCache && !r) {
			var sf = arguments.callee;
			this._get_(function(ret) {
				this._set_cache(ret);
				sf.apply(this, [true]);
			}.bind(this));
			return;
		}
		if(this.mobile_status == 0){
			var t = new Template(mobileSmsOptionTemplate);
			var data = [];
			if(this.mobile_payPermsg == 0) {
				data['mobile_limit'] = '<dd> <input name="mobile_limit" id="mobile_limit_n" type="radio" value="mobile_limit" checked="checked" /> 不提醒我 </dd> <dd> <input type="radio" name="mobile_limit" id="mobile_limit_y" value="mobile_limit" /> 提醒我 （此选项由于移动政策，暂时失效）</dd>';
			} else {
				data['mobile_limit'] = '<dd> <input name="mobile_limit" id="mobile_limit_n" type="radio" value="mobile_limit" /> 不提醒我 </dd> <dd> <input type="radio" name="mobile_limit" value="mobile_limit" id="mobile_limit_y" checked="checked" /> 提醒我 （此选项由于移动政策，暂时失效）</dd>';
			}
			if(this.mobile_onTimeAlert == 1) {
				data['mobile_range'] = '<dd> <input name="mobile_range" id="mobile_range_n" type="radio" value="mobile_range" checked="checked" /> 8：00——23：00之间提醒 </dd><dd> <input name="mobile_range" id="mobile_range_y" type="radio" value="mobile_range" /> 在不超过限制条数的情况下全天候提醒</dd>';
			} else {
				data['mobile_range'] = '<dd> <input name="mobile_range" id="mobile_range_n" type="radio" value="mobile_range" /> 8：00——23：00之间提醒 </dd><dd> <input name="mobile_range" type="radio" id="mobile_range_y" value="mobile_range" checked="checked" /> 在不超过限制条数的情况下全天候提醒</dd>';
			}
			var tt = t.evaluate(data);
			hiddenAllPanel();
			$('mobileConfiguration').update(tt);
			$('mobileConfiguration').show();
			// dhtmlHistory.add("mobileGetSmsOption", "mobileCfg.getSmsOption();", "手机邮件高级选项");
		}else{
			// alert("请先开通手机邮件");
			_dialog.alert({info: '请先开通手机邮件'});
		}
	},
	
	setSms: function() {
		var allsms, notsms, fromlist, subjectlist;
		if($('mobile_remind_y').checked) {
			notsms = "false";
		} else {
			notsms = "true";
		}
		fromlist = $('mobile_filter').value;
		subjectlist = $('mobile_subject').value;
		var params = {
			allsms: allsms,
			notsms: notsms,
			fromlist: fromlist,
			subjectlist: subjectlist
		};
		sms_post(params, this.setSmsCallBack.bind(this));
	},
	setSmsCallBack: function(ret) {
		if(ret.status == 0) {
			this.getMobile();
		} else {
			// alert("信息设置失败！");
			_dialog.alert({info: '信息设置失败！'});
		}
	},
	
	setSmsOption: function() {
		var OnTimeAlert, PayPermsg;
		if($('mobile_limit_n').checked) {
			PayPermsg = 0;
		} else {
			PayPermsg = 1;
		}
		if($('mobile_range_n').checked) {
			OnTimeAlert = 1;
		} else {
			OnTimeAlert = 0;
		}
		var params = {
				PayPermsg: PayPermsg	,
				OnTimeAlert: OnTimeAlert			
		};
		smsoption_post(params, this.setSmsOptionCallBack.bind(this));
	},
	setSmsOptionCallBack: function(ret) {
		if(ret.status == 0) {
			this.getMobile();
		} else {
			// alert("信息设置失败！");
			_dialog.alert({info: '信息设置失败！'});
		}
	},
	
	// 开通手机邮件服务
	openMobileService: function() {
		var url = 'http://mail.sohu.com/momail/mobileOrder.html';
		var property = 'width=642,height=450,scrollbars=auto';
		if (USE_SYS.name=="vip.sohu"){
			url = "http://vip.sohu.com/confirmMethodAction.action?pay_method=2";
			property=null;
		}
		window.open(url,'mobilemail',property);
	},
	// 查询状态
	searchMobileStatus: function() {
		if(this.mobile_status == 0){
			window.open('http://mail.sohu.com/momail/mobileQuery.jsp','querymomail','width=642,height=300,scrollbars=auto');
		}else{
			// alert("请先开通手机邮件");
			_dialog.alert({info: '请先开通手机邮件'});
		}
	},
	// 取消服务
	closeMobileService: function() {
		if(this.mobile_status == 0) {
			var url = 'http://mail.sohu.com/momail/mobileCancel.html';
			var property = 'width=642,height=450,scrollbars=auto';
			if(USE_SYS.name=="vip.sohu"){
				url="http://vip.sohu.com/mo/vipmail/momailcancel.jsp";
				property = null;
			}
			window.open(url,'momail_unsub',property);
		} else {
			// alert("请先开通手机邮件");
			_dialog.alert({info: '请先开通手机邮件'});
		}	
	},
	//变更手机号
	changeMoBile:function(){
		var url = 'http://mail.sohu.com/momail/mobileCp.jsp';
		var property = 'width=642,height=450,scrollbars=auto';
		if(USE_SYS.name=="vip.sohu"){
			url="http://vip.sohu.com/mo/vipmail/momailCp.jsp";
			property = null;
		}
		window.open(url,'momail_unsub',property);
	}
};
var mobileCfg = new MobileCfg();